import logging
import time
from logging.handlers import RotatingFileHandler
'''只打印到屏幕或文件夹'''
# --------------basicConfig-----------------
'''
logging.basicConfig(filename='../logs/loging.logs',
format="%(asctime)s %(filename)s \
%(funcName)s [%(lineno)d]--%(levelno)s- %(message)s",
filemode='a',
level="DEBUG")
# %(levelno)s：打印日志级别的数值
# %(levelname)s：打印日志级别的名称
# %(pathname)s：打印当前执行程序的路径，其实就是sys.argv[0]
# %(filename)s：打印当前执行程序名
# %(funcName)s：打印日志的当前函数
# %(lineno)d：打印日志的当前行号
# %(asctime)s：打印日志的时间
# %(thread)d：打印线程ID
# %(threadName)s：打印线程名称
# %(process)d：打印进程ID
# %(message)s：打印日志信息
# ---------------------------------------------
logging.debug('debug message')
logging.info('info message')
logging.warning('waaring message')
logging.error('error message')
logging.critical('critical message')
'''
class LOG:
    def __init__(self, level=None, message=None):
        self.level = level
        self.message = message
        self.logger = logging.getLogger()
        self.logger.setLevel = "DEBUG"
        # self.logfile = os.path.join(logpath, '%s.logs' % time.strftime('%Y-%m-%d %H-%M-%S'))
        self.fm = logging.Formatter('%(asctime)s %(filename)s %(funcName)s [%(lineno)d]- %(levelno)d %(message)s')
    def __logfunction(self,level, message):
        # '''同时输出日志到屏幕和文件中 '''
        ''' “S”: Seconds、“M”: Minutes、“H”: Hours、“D”: Days,“W”: Week day (0=Monday)、
        “midnight”: Roll over at midnight,interval: 滚动周期，单位有when指定，
        比如：when=’D’,interval=1，表示每天产生一个日志文件；backupCount: 表示日志文件的保留个数；
        '''
        fh = RotatingFileHandler("../logs/mylog.log", mode="a", maxBytes=1 * 1024, backupCount=2)
        # fh = logging.FileHandler(self.logfile, mode="a")
        ch = logging.StreamHandler()
        fh.setFormatter(self.fm)
        ch.setFormatter(self.fm)
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

        if self.level == 'info':
            self.logger.info(message)
        elif self.level == 'debug':
            self.logger.debug(message)
        elif self.level == 'warning':
            self.logger.warning(message)
        elif self.level == 'error':
            self.logger.error(message)
        else:
            self.logger.critical(message)

        self.logger.removeHandler(ch)
        self.logger.removeHandler(fh)
        fh.close()

    def debug(self, message):
        self.__logfunction('debug', message)

    def info(self, message):
        self.__logfunction('info', message)

    def warning(self, message):
        self.__logfunction('warning', message)

    def error(self, message):
        self.__logfunction('error', message)

    def critical(self, message):
        self.__logfunction('critical', message)


class Tlog:

    def run(self):
        self.log = LOG()
        self.log.debug("debug messaga")
        time.sleep(2)
        self.log.info("info messaga")
        time.sleep(2)
        self.log.warning("waring messaga")
        time.sleep(2)
        self.log.error("error messaga")
        time.sleep(2)
        self.log.critical("critical messaga")


if __name__ == "__main__":
    for i in range(100):
        t = Tlog()
        t.run()
